package com.wpml.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpml.common.util.PageUtils;
import com.wpml.common.util.Query;
import com.wpml.product.dao.SpuImagesDao;
import com.wpml.product.entity.SpuImagesModel;
import com.wpml.product.service.SpuImagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * spu图片(PmsSpuImages)表服务实现类
 *
 * @author makejava
 * @since 2020-04-07 11:33:37
 */
@Service
@Slf4j
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesDao, SpuImagesModel> implements SpuImagesService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuImagesModel> page = this.page(
                new Query<SpuImagesModel>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveImages(Long id, List<String> images) {
        if (CollUtil.isNotEmpty(images)) {
            List<SpuImagesModel> collect = images.stream().map(img -> {
                SpuImagesModel spuImagesEntity = new SpuImagesModel();
                spuImagesEntity.setSpuId(id);
                spuImagesEntity.setImgUrl(img);

                return spuImagesEntity;
            }).collect(Collectors.toList());

            this.saveBatch(collect);
        }
    }
}
